home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4349 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.7 KB  |  130 lines

  1. Path: news.infoserve.net!usenet
  2. From: paveltka@unix.infoserve.net (Pavel Tkatchouk)
  3. Newsgroups: comp.lang.c++
  4. Subject: Stupid Q: what's wrong?
  5. Date: Mon, 29 Jan 1996 21:34:11 GMT
  6. Message-ID: <4ejerd$r84@news.infoserve.net>
  7. NNTP-Posting-Host: unix.infoserve.net
  8. X-Newsreader: Forte Free Agent v0.55
  9.  
  10. Hi, there:
  11. I'm trying to write a simple program for extracting coordinates from
  12. text file, shifting them and writing back to a new text file. The
  13. prototype of the program shown below 
  14. gives different coordinates (which I believe shouldn't be) depending
  15. of:
  16.  
  17. WITH line #25 output is:
  18.  
  19. N0021 X3.274Y-1.25Z.0787R.55F5.  
  20.  iCount=0
  21.  iCount=2
  22. N0021 X4.274Y-1.25Z3.0787R.55F5.
  23.  fD[0]: 1 fD[1]: 0 fD[2]: 3
  24.  
  25.  
  26.  
  27. WITHOUT line #25 output is:
  28.  
  29. N0021 X3.274Y-1.25Z.0787R.55F5.
  30. N0021 X4.274Y1.75Z.0787R.55F5.
  31.                          ^^^^
  32.  fD[0]: 1 fD[1]: 0 fD[2]: 3
  33.  
  34.  
  35. My questions are:
  36. 1. Why  cout<<"\n iCount="<<iCount   affects CoordShift()?
  37. 2. What would you recommend me to change in my prototype to make it
  38. more efficient? 
  39.  
  40. Thank you,
  41.  
  42. Pavel 
  43.  
  44. ---begin---
  45. //Test to extract coordinates from string, shift and write back
  46. #include <fstream.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. void CoordShift(char *, char *, float);
  50. main ()
  51. {
  52.     char szFileLine[80], szX_Coord[9]="",szN_Coord[9]="",
  53.     szY_Coord[9]="", szZ_Coord[9]="", *ptr;
  54.     char c, szAxis[]="XYZ";//Coordinate names to be shifted
  55.     int iInd, iCount;
  56.     float  fD[]={1.0,0.0,3.0}, fShift;//To shift X+fD[0], etc.
  57.  
  58.                                                   //will be entered
  59.  
  60.                                                   //from keyboard in 
  61.                                                  //next version
  62.  
  63.     double dX_Coord, 
  64.                 dY_Coord, 
  65.                 dZ_Coord;//Numeric equivalent of 
  66.                                 //strings szX_Coord, etc.
  67.  
  68.     strcpy(szFileLine,"N0021 X3.274Y-1.25Z.0787R.55F5.");//for debug 
  69.                           //only, szFileLine will be read from text
  70.  
  71.                          //file later
  72.     cout<<"\n"<<szFileLine;//Debug, line before coordinate shifting
  73.  
  74.     for (iCount=0;iCount<=2;iCount++)
  75.     {
  76.         if(fD[iCount])
  77.         {
  78.  //        cout<<"\n iCount="<<iCount;//line #25, !!!!!!!!!
  79.             CoordShift(szFileLine,szAxis+iCount,fD[iCount]);
  80.         }
  81.     }
  82.     cout<<"\n"<<szFileLine;//Debug, line after coordinate shifting
  83.     cout<<"\n fD[0]: "<<fD[0]<<"  fD[1]: "<<fD[1]<<" fD[2]:   
  84.     "<<fD[2];//Debug
  85.     return 0;
  86. }
  87. void CoordShift(char *szF_L,//Line with coordinates to be extracted
  88.                 char *szAx,// axis name to be shifted
  89.                 float fSh)//the value of shifting
  90. {
  91.     char szRest[90];//This part of the line shouldn't be changed
  92.     char *ptr,
  93.      *pC_Beg,//Here starts the coordinate to be shifted
  94.      *blank="", c, szA, szC[10];
  95.     double dC;
  96.     szA=*szAx;
  97.     strcpy(szC,blank);
  98.     ptr = strpbrk(szF_L,&szA);//Where the coordinate starts?
  99.     if(ptr)
  100.     {
  101.         ptr++;
  102.         pC_Beg=ptr;
  103.         c=*ptr;
  104.         while((c>=48&&c<=57)||//while c is digit,-,+ or .
  105.                                (c==43)||
  106.             (c==45)||
  107.             (c==46))
  108.         {
  109.             c= *ptr;
  110.             strcat(szC,&c);
  111.            *(szC + strlen(szC)-1)=*"\0";
  112.            ptr++;
  113.         }
  114.         strcpy(szRest,ptr-1);
  115.         *strpbrk(szC,"XYZR")=*"\0";//to eliminate X,Y,Z at the end of 
  116.                                                     //the szC
  117.         dC=atof(szC);
  118.         dC +=fSh;
  119.         gcvt(dC,6,szC);
  120.         strcpy(pC_Beg,szC);//writing shifted coordinate back to the 
  121.                                        //original line 
  122.         strcpy(pC_Beg + strlen(szC),szRest);//writing unchanged rest
  123.  
  124.                                             //after the new coordinate
  125.     }
  126. }
  127. ---end---
  128.  
  129.  
  130.